package com.fyt.service.Impl.pasture;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fyt.controller.utils.Code;
import com.fyt.controller.utils.Result;
import com.fyt.entity.City;
import com.fyt.entity.Pasture;
import com.fyt.mapper.CityMapper;
import com.fyt.service.pasture.CityService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.sound.midi.SoundbankResource;
import java.util.*;

/**
 * <p>
 * 基础表-省市区 服务实现类
 * </p>
 *
 * @author gdy
 * @since 2023-12-20
 */
@Service
public class CityServiceImpl extends ServiceImpl<CityMapper, City> implements CityService {

    @Autowired
    private CityMapper cityMapper;

    @Override
    public Result getCityByLevel(Map<String, String> map) {
        String name = map.get("name");
        String level = map.get("level");

        Map<String, Object> responseMap = new HashMap<>();
        List<String> result = new ArrayList<>();

        if (StringUtils.isEmpty(name)) {
            QueryWrapper<City> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().select(City::getName).eq(City::getLevel, 1);
            List<City> provinceList = cityMapper.selectList(queryWrapper);
            for (City city : provinceList) {
                result.add(city.getName());
            }
            responseMap.put("province", result);
        } else {
            QueryWrapper<City> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().select(City::getId).eq(City::getName, name);

            City city = cityMapper.selectOne(queryWrapper);

            if (!Objects.isNull(city)) {
                QueryWrapper<City> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.lambda().select(City::getId, City::getName).eq(City::getParentId, city.getId());
                List<City> cityList = cityMapper.selectList(queryWrapper1);
                for (City city1 : cityList) {
                    result.add(city1.getName());
                }
                responseMap.put("city", result);
            } else {
                QueryWrapper<City> queryWrapper2 = new QueryWrapper<>();
                queryWrapper2.lambda().select(City::getId).eq(City::getName, name).eq(City::getLevel, 2);
                City districtCity = cityMapper.selectOne(queryWrapper2);

                if (!Objects.isNull(districtCity)) {
                    LambdaQueryWrapper<City> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.select(City::getName).eq(City::getParentId, districtCity.getId());
                    List<City> districtList = cityMapper.selectList(lambdaQueryWrapper);
                    for (City city1 : districtList) {
                        result.add(city1.getName());
                    }
                    responseMap.put("districtList", result);
                }
            }
        }

        return new Result(responseMap, Code.save_ok, "success");
    }

}
